<?php

/**
 * @category   mediad2
 * @package    Mediad2_Service
 */
class Mediad2_Service_Admin_RolePermission extends Mediad2_Service_Abstract
{
	/**
	 * 初始化 规则
	 */
	public function init()
	{
		$this->setMapper('Mediad2_Mapper_RolePermission');
		$this->setModel('Mediad2_Model_RolePermission');
	}

	/**
	 * 返回指定角色的规则总数
	 * 
	 * @param string $roleCode
	 * @return int The number of rows
	 */
	public function getCountRuleByRole($roleCode = null)
	{
		$mapper = $this->getMapper();
		$condition = null;
		if (!null == $roleCode) {
			$selector = $mapper->getDao()->select();
			$adapter = $mapper->getDao()->getAdapter();
			$parms = array( 'role'=>$roleCode, );
			foreach ($parms as $key=>$value) {
				$selector->where($adapter->quoteInto("$key=?",$value));
			}
			$condition = implode(' ', $selector->getPart('where'));
		}
		$result = $mapper->getCount($condition);
		return $result;
	}

	/**
	 * 批量删除指定的规则记录
	 *
	 * @param array of $ruleId
	 * @return boolean return true if no error.
	 */
	public function deleteBatch(array $arrayRuleId)
	{
		if ( count($arrayRuleId) == 0 ) {
			return false;
		}
	
		//构造where条件
		$where = 'role2PermissionId in ( ' . implode(",", $arrayRuleId) . ')';
	
		$mapper = $this->getMapper();
		$dao = $mapper->getDao();
		$result = $dao->delete($where);
		if ($result > 0) {
			return true;
		} else {
			return false;
		}
	}
	
	
}